const express = require('express');
const router = express.Router();
const upload = require('../lib/upload.js');

const { TeamModel } = require('../model/team.js');

router.get('/', (req, res) => {
    TeamModel.find({
    }).then(teams => {
       res.json(teams); 
    });
});

router.post('/create', upload.single('icon'), (req, res)=> {
    const teamData = {
        name: req.body.name,
        slogan: req.body.slogan,
        icon: req.file ? req.file.path : null,
        homeStadium: req.body.homeStadium,    
    };
    TeamModel.create(teamData).then((result) => {
        res.status(201).json(result);
    }).catch(err => {
        console.error(err);
        res.status(500).send('Error creating team');
    }); 
});

module.exports = router;
